package com.sp.outputstream_;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 编程完成 图片/音乐 的拷贝
 */
public class FileCopy {
    public static void main(String[] args) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        String filePath1 = "d:\\development\\code\\IO_Test\\news\\1.jpg";
        String filePath2 = "d:\\development\\code\\IO_Test\\news\\2.jpg";

        try {
            fileInputStream = new FileInputStream(filePath1);
            fileOutputStream = new FileOutputStream(filePath2);
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = fileInputStream.read(buf)) != -1){
                //读取到后,就写入到文件 通过fileOutputStream
                fileOutputStream.write(buf, 0, readLen);//一定要用这个方法
            }
            System.out.println("拷贝成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null){
                    fileInputStream.close();
                }
                if (fileOutputStream != null){
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
